﻿Imports QLPhuTungOto.Data
Public Class PhuTungBUS
    Public Function LayBang()
        Dim db As New QLPhuTungOtoDataContext

        Dim q = From pt In db.PhuTung Join lpt In db.LoaiPhuTung On pt.MaLoaiPhuTung Equals lpt.MaLoaiPhuTung Join x In db.Xe On pt.MaXe Equals x.MaXe Join ncc In db.NhaCungCap On pt.MaNhaCungCap Equals ncc.MaNhaCungCap Join ha In db.HinhAnh On pt.MaHinhAnh Equals ha.MaHinhAnh
                Where pt.IsDelete = 0
                Select pt.Stt, pt.MaPhuTung, pt.MaLoaiPhuTung, pt.TenPhuTung, pt.MaNhaCungCap, pt.SoLuong, pt.GiaNhap, pt.GiaBan, pt.NgayNhap, pt.MaXe, pt.DonVi, lpt.TenLoaiPhuTung, x.TenXe, ncc.TenNhaCungCap, ha.DuongDan

        Return q
    End Function

    Public Function LayBang(ByVal _dongBatDau As Integer, ByVal _soLuong As Integer)
        Dim db As New QLPhuTungOtoDataContext
        Dim query As IEnumerable(Of Object)
        query = (From pt In db.PhuTung Join lpt In db.LoaiPhuTung On pt.MaLoaiPhuTung Equals lpt.MaLoaiPhuTung Join x In db.Xe On pt.MaXe Equals x.MaXe Join ncc In db.NhaCungCap On pt.MaNhaCungCap Equals ncc.MaNhaCungCap Join ha In db.HinhAnh On pt.MaHinhAnh Equals ha.MaHinhAnh
                Where (pt.IsDelete = 0)
                Order By pt.Stt
                Select pt.Stt, pt.MaPhuTung, pt.MaLoaiPhuTung, pt.TenPhuTung, pt.MaNhaCungCap, pt.SoLuong, pt.GiaNhap, pt.GiaBan, pt.NgayNhap, pt.MaXe, pt.DonVi, lpt.TenLoaiPhuTung, x.TenXe, ncc.TenNhaCungCap, ha.DuongDan)
        Return query.Skip(_dongBatDau).Take(_soLuong)
    End Function

    Public Function LoadDSPhuTung()
        Dim temp As New PhuTung()
        Return temp.LoadDSPhuTung()
    End Function

    Public Function LoadDSPhuTung(ByVal pt As PhuTung, ByVal _dongBatDau As Integer, ByVal _soLuong As Integer)
        Dim temp As New PhuTung()
        Return temp.LoadDSPhuTung(_dongBatDau, _soLuong)
    End Function

    Public Sub Them(ByVal pt As PhuTung)
        Dim db As New QLPhuTungOtoDataContext
        Dim a As PhuTung
        Try
            a = db.PhuTung.GetByKey(pt.MaPhuTung)
        Catch ex As Exception

        End Try

        If IsNothing(a) Then
            pt.IsDelete = 0
            db.PhuTung.InsertOnSubmit(pt)
            db.SubmitChanges()
        ElseIf a.MaPhuTung.ToString() = pt.MaPhuTung.ToString() Then
            a.IsDelete = 0
            db.SubmitChanges()
        End If

    End Sub

    Public Sub Xoa(ByVal pt As PhuTung)
        Dim db As New QLPhuTungOtoDataContext
        'db.PhuTung.Delete(pt.MaPhuTung)
        Try
            Dim a As New PhuTung
            a = db.PhuTung.GetByKey(pt.MaPhuTung)
            a.IsDelete = 1
            db.SubmitChanges()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub Sua(ByVal pt As PhuTung)
        Dim db As New QLPhuTungOtoDataContext
        Dim _PhuTung As New PhuTung

        '_PhuTung = (From a In db.PhuTung
        '           Where a.MaPhuTung = pt.MaPhuTung
        '           Select a).First()

        _PhuTung = db.PhuTung.GetByKey(pt.MaPhuTung)

        _PhuTung.MaLoaiPhuTung = pt.MaLoaiPhuTung
        _PhuTung.TenPhuTung = pt.TenPhuTung
        _PhuTung.MaNhaCungCap = pt.MaNhaCungCap
        _PhuTung.MaHinhAnh = 1
        _PhuTung.SoLuong = pt.SoLuong
        _PhuTung.GiaNhap = pt.GiaNhap
        _PhuTung.GiaBan = pt.GiaBan
        _PhuTung.NgayNhap = pt.NgayNhap
        _PhuTung.MaXe = pt.MaXe
        _PhuTung.DonVi = pt.DonVi
        _PhuTung.IsDelete = 0

        db.SubmitChanges()
    End Sub

    Public Function LayTheoMa(ByVal maPhuTung As String) As PhuTung
        Dim pt As New PhuTung
        Dim db As New QLPhuTungOtoDataContext
        Try
            'pt = db.PhuTung.GetByKey(maPhuTung)
            pt = (From a In db.PhuTung Where a.MaPhuTung = maPhuTung AndAlso a.IsDelete = 0 Select a).First()
        Catch ex As Exception

        End Try
        Return pt
    End Function

    Public Sub CapNhatSoLuong(ByVal l As List(Of ChiTietHoaDon))
        Dim db As New QLPhuTungOtoDataContext
        Dim temp As PhuTung
        temp.UpdatePhuTung(l)
    End Sub

    Public Sub ThemSoLuongPhuTung(ByVal mapt As String, ByVal sl As Integer)
        Dim db As New QLPhuTungOtoDataContext
        db.PhuTung.ThemSoLuongPhuTung(mapt, sl)
    End Sub

    Public Function DemSoLuongPhuTung() As Integer
        Dim sl As Integer = 0
        Dim db As New QLPhuTungOtoDataContext
        sl = (From pt In db.PhuTung Where pt.IsDelete = 0 Select pt).Count
        Return sl
    End Function

    Public Sub CapNhatGiaPhuTung(ByVal mapt As String, ByVal gianhap As Double, ByVal giaban As Double)
        Dim db As New QLPhuTungOtoDataContext
        db.PhuTung.CapNhatGiaPhuTung(mapt, gianhap, giaban)
    End Sub
End Class
